function loadXMLDoc(dname)
{
    if (window.XMLHttpRequest)
    {
        xhttp=new XMLHttpRequest();
    }
    else
    {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET",dname,false);
    xhttp.send("");
    return xhttp.responseXML;
}



function nsResolver(prefix) {
    return prefix == 'n' ? 'http://xml.netbeans.org/schema/informations' : null;
}
function search(id){

    var searchValue = document.getElementById("txtSearchValue").value;
    // Delete childNode;
    deleteDiv(searchValue);
    var xml=loadXMLDoc("xml/information.xml");
    
    var path="//n:information[n:id = '"+id+"']";
    // code for IE
    if (window.ActiveXObject)
    {
        var nodes=xml.selectNodes(path);

        for (i=0;i<nodes.length;i++)
        {
            document.write(nodes[i].childNodes[0].nodeValue);
            document.write("<br>");
        }
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument) {
        var nodes = xml.evaluate(path, xml, nsResolver, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
        var array = new Array();
        //while (result) {
            array[0] = result.getElementsByTagName("id")[0].childNodes[0].nodeValue;
            array[1] = result.getElementsByTagName("owner")[0].childNodes[0].nodeValue;
            array[2] = result.getElementsByTagName("info")[0].childNodes[0].nodeValue;
            array[3] = result.getElementsByTagName("type")[0].childNodes[0].nodeValue;
            array[4] = result.getElementsByTagName("date")[0].childNodes[0].nodeValue;
            array[5] = result.getElementsByTagName("category")[0].childNodes[0].nodeValue;
            addDiv(array);
          //  result = nodes.iterateNext();
        //}

    }
}

function searchByType(type){


    // Delete childNode;
    deleteDiv(type);

    var xml=loadXMLDoc("xml/information.xml");
    //var searchValue = document.getElementById("txtSearchValue").value;
    var path="//n:information[contains(n:type,'"+type+"')]";
    // code for IE
    if (window.ActiveXObject)
    {
        var nodes=xml.selectNodes(path);

        for (i=0;i<nodes.length;i++)
        {
            document.write(nodes[i].childNodes[0].nodeValue);
            document.write("<br>");
        }
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument) {
        var nodes = xml.evaluate(path, xml, nsResolver, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
        var array = new Array();
        while (result) {
            array[0] = result.getElementsByTagName("id")[0].childNodes[0].nodeValue;
            array[1] = result.getElementsByTagName("owner")[0].childNodes[0].nodeValue;
            array[2] = result.getElementsByTagName("info")[0].childNodes[0].nodeValue;
            array[3] = result.getElementsByTagName("type")[0].childNodes[0].nodeValue;
            array[4] = result.getElementsByTagName("date")[0].childNodes[0].nodeValue;
            array[5] = result.getElementsByTagName("category")[0].childNodes[0].nodeValue;
            addDiv(array);
            result = nodes.iterateNext();
        }

    }
}

function addDiv(array){
    var div = document.createElement("div");
    div.setAttribute("class", "question_list");
    div.setAttribute("id", "question");
    div.innerHTML = "<h3><a href='detail.jsp?id="+array[0]+"' rel='bookmark'>"+array[5]+ array[0]+ " - "+ array[1] + " - "+ array[2] +"</a></h3><span class='post-time'>"+array[4]+"</span><br/><p><span class='pcate'>  <a href='http://www.timgiayto.com/c/bang-lai-xe'>"+array[3]+"</a></span></p>";
    var pa = document.getElementById("parent");
    pa.appendChild(div);
}

function deleteDiv(value){
    var parent = document.getElementById("parent");
    parent.innerHTML = "";
    var h1 = document.createElement("h1");
    h1.setAttribute("class", "page_head");
    //var searchValue = document.getElementById("txtSearchValue").value;
    h1.innerHTML = "Kết quả tìm kiếm - " + value;
    var pa = document.getElementById("parent");
    pa.appendChild(h1);
}

function convertVietnamese(str){
    
    str= str.toLowerCase();
    str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");
    str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");
    str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");
    str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");
    str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");
    str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");
    str= str.replace(/đ/g,"d");
    return str;
}

function searchInFileConvert(){
    var str = document.getElementById("txtSearchValue").value;
    var searchValue = convertVietnamese(str);
    //var searchValue = document.getElementById("txtSearchValue").value;
    // Delete childNode;
    //deleteDiv(searchValue);
    var xml=loadXMLDoc("xml/information_convert.xml");

    var path="//n:information[contains(n:owner,'"+searchValue+"') or contains(n:info,'"+searchValue+"')]";
    // code for IE
    if (window.ActiveXObject)
    {
        var nodes=xml.selectNodes(path);

        for (i=0;i<nodes.length;i++)
        {
            document.write(nodes[i].childNodes[0].nodeValue);
            document.write("<br>");
        }
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument) {
        var nodes = xml.evaluate(path, xml, nsResolver, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
        //var array = new Array();
        while (result) {
            var id = result.getElementsByTagName("id")[0].childNodes[0].nodeValue;
            search(id);
//            array[1] = result.getElementsByTagName("owner")[0].childNodes[0].nodeValue;
//            array[2] = result.getElementsByTagName("info")[0].childNodes[0].nodeValue;
//            array[3] = result.getElementsByTagName("type")[0].childNodes[0].nodeValue;
//            array[4] = result.getElementsByTagName("date")[0].childNodes[0].nodeValue;
//            array[5] = result.getElementsByTagName("category")[0].childNodes[0].nodeValue;
            //addDiv(array);
            result = nodes.iterateNext();
        }
        
    }
}


